package com.mybatis.jpa.common;

import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/***
 * 关联性字段工具类
 *
 * @author lishiwei
 * @data 2017年8月10日
 *
 */
public class AssociationUtil {

    public static Class<?> getTargetType(Field field) {
        if (field.isAnnotationPresent(OneToOne.class)) {
            // 获取注解对象
            OneToOne one = field.getAnnotation(OneToOne.class);
            // 设置了targetEntity属性
            if (!one.targetEntity().equals(void.class)) {
                return one.targetEntity();
            }
        }
        return void.class;
    }

    public static String getMappedName(Field field) {
        if (field.isAnnotationPresent(OneToOne.class)) {
            // 获取注解对象
            OneToOne one = field.getAnnotation(OneToOne.class);
            // 设置了mappedBy()属性
            if (!one.mappedBy().trim().equals("")) {
                return one.mappedBy();
            }
        }
        if (field.isAnnotationPresent(OneToMany.class)) {
            // 获取注解对象
            OneToMany many = field.getAnnotation(OneToMany.class);
            // 设置了mappedBy()属性
            if (!many.mappedBy().trim().equals("")) {
                return many.mappedBy();
            }
        }
        return null;
    }

    /**
     * 获取关联性字段</br>
     *
     * @param clazz class对象
     * @return
     */
    public static List<Field> getAssociationFields(Class<?> clazz) {
        List<Field> list = new ArrayList<>();
        Class<?> searchType = clazz;
        while (!Object.class.equals(searchType) && searchType != null) {
            Field[] fields = searchType.getDeclaredFields();
            for (Field field : fields) {
                if (isAssociationField(field)) {
                    list.add(field);
                }
            }
            searchType = searchType.getSuperclass();
        }
        return list;
    }


    /**
     * 获取关联性字段</br>
     *
     * @param clazz class对象
     * @return
     */
    public static Field getPkFields(Class<?> clazz) {
        Class<?> searchType = clazz;
        while (!Object.class.equals(searchType) && searchType != null) {
            Field[] fields = searchType.getDeclaredFields();
            for (Field field : fields) {
                if (isPkField(field)) {
                    return field;
                }
            }
            searchType = searchType.getSuperclass();
        }
        return null;
    }

    /**
     * 是否为关联性字段</br>
     * javax.persistence.OneToOne注解</br>
     *
     * @param field Field对象
     */
    public static boolean isAssociationField(Field field) {
        return field.isAnnotationPresent(OneToOne.class) || field.isAnnotationPresent(OneToMany.class);
    }

    /**
     * 是否为主键字段</br>
     * javax.persistence.OneToOne注解</br>
     *
     * @param field Field对象
     */
    public static boolean isPkField(Field field) {
        return field.isAnnotationPresent(Id.class);
    }

}
